package com.yellow.userservice.controller;

import com.yellow.userservice.entity.User;
import com.yellow.userservice.service.UserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author: hy
 * @create: 2024-03-29
 */

@Slf4j
@RestController
@Tag(name="注册",description = "注册逻辑")
@CrossOrigin(origins = "*",maxAge =3600 )
public class RegisterController {

    @Resource
    private UserService userService; // 假设你有一个Service来处理用户逻辑

    @PostMapping("/register")
    public ResponseEntity<?> registerUser(@RequestBody Map<String,String> user) {
        // 这里应该包括一些逻辑来处理用户注册，比如检查用户名是否已存在等
        // 假设 userService.register(user) 返回一个包含用户信息的实例如果注册成功
        log.info("user = "+user);
        User curUser = new User(null,
                user.get("nickname"),
                user.get("password"),
                user.get("email"),
                "customer",
                user.get("phoneNumber"),
                user.get("avatar"));
        boolean flag = userService.registerUser(curUser);
        if (flag) {
            return ResponseEntity.ok().body(user);
        } else {
            return ResponseEntity.badRequest().body("注册失败，用户可能已存在。");
        }
    }
}
